package org.itboys.mobile.service.mysql.site;

import org.itboys.admin.entity.lasted.SystemDictionary;
import org.itboys.admin.service.lasted.SystemDictionaryService;
import org.itboys.commons.ErrorException;
import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.mobile.entity.mongo.SundayMemberRecord;
import org.itboys.mobile.entity.mysql.product.SundayProduct;
import org.itboys.mobile.entity.mysql.product.SundayProductRecord;
import org.itboys.mobile.service.mongo.site.SundaySiteMemberRecordService;
import org.itboys.mobile.service.mongo.web.SundayPermissionService;
import org.itboys.mobile.service.mongo.web.SundayRecordPermissionService;
import org.itboys.mysql.service.BaseMySqlService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者：zzq
 * 时间：2017/10/25
 * 描述：雄安加盟网_项目记录相关
 */
@Service
public class SundaySiteProductRecordService extends BaseMySqlService<SundayProductRecord>{
    @Autowired
    private SundaySiteMemberRecordService memberRecordService;
    @Autowired
    private SundayRecordPermissionService recordPermissionService;
    @Autowired
    private SystemDictionaryService dictionaryService;
    /**
     * 项目数据维护更新
     * @param productId
     * @param joinFee
     * @param joinAdvantage
     * @param joinProcess
     */
    public void updateData(Long productId,String joinFee,String joinAdvantage,String joinProcess,String detail){
        SundayProductRecord product=super.find(productId);
        product.setJoinFee(joinFee);
        product.setJoinAdvantage(joinAdvantage);
        product.setJoinProcess(joinProcess);
        product.setDetail(detail);
        super.update(product);
        //更新用户记录状态
        SundayMemberRecord memberRecord=memberRecordService.getById(recordPermissionService.getMemberRecord(productId));
        memberRecord.setStatus(1);
        memberRecordService.updateExceptEmpty(memberRecord.getId(),memberRecord);
    }
    /**
     * 根据企业返回该企业的项目
     * @param memberRecordId
     * @return
     */
    public SundayProductRecord findOneByMember(Long memberRecordId){
        Long productRecordId=0l;
        SundayMemberRecord memberRecord=memberRecordService.getById(memberRecordId);
        List<Long> otherIds=recordPermissionService.getRecordPermission(memberRecordId,1);
        if(ListUtil.isNotNull(otherIds)){
            productRecordId=otherIds.get(0);
        }else {
            throw  new ErrorException("系统异常");
        }

        return super.find(productRecordId);
    }

    public SundayProductRecord packageTags(SundayProductRecord productRecord,  boolean isAll){

        //组装项目招商模式标签
        List<SystemDictionary> joinModelDictionaries=dictionaryService.getDictionaryByKey("join_model");
        if(productRecord.getJoinModel()==null)productRecord.setJoinModel("");
        String [] joinModelTagIds = productRecord.getJoinModel().split(",");

        List<Map<String,Object>> joinModelTags = new ArrayList<Map<String, Object>>();
        for(SystemDictionary dictionary:joinModelDictionaries){
            Map<String,Object> tag=new HashMap<String,Object>();
            tag.put("id",dictionary.getId());
            tag.put("name",dictionary.getValue());
            int isChecked=0;

            for(String modelTagId:joinModelTagIds){
                if(modelTagId.equalsIgnoreCase(String.valueOf(dictionary.getId()))){
                    isChecked=1;
                    break;
                }
            }
            tag.put("isChecked",isChecked);
            if(!isAll){
                if(isChecked==1)joinModelTags.add(tag);
            }else{
                joinModelTags.add(tag);
            }

        }
        productRecord.setJoinModelTags(joinModelTags);

        //组装项目经营模式标签
        List<SystemDictionary> managerModelDictionaries=dictionaryService.getDictionaryByKey("manager_model");
        if(productRecord.getManagerModel()==null)productRecord.setManagerModel("");
        String [] managerModelTagIds = productRecord.getManagerModel().split(",");

        List<Map<String,Object>> managerModelTags = new ArrayList<Map<String, Object>>();
        for(SystemDictionary dictionary:managerModelDictionaries){
            Map<String,Object> tag=new HashMap<String,Object>();
            tag.put("id",dictionary.getId());
            tag.put("name",dictionary.getValue());
            int isChecked=0;

            for(String managerTagId:managerModelTagIds){
                if(managerTagId.equalsIgnoreCase(String.valueOf(dictionary.getId()))){
                    isChecked=1;
                    break;
                }
            }
            tag.put("isChecked",isChecked);
            if(!isAll){
                if(isChecked==1)managerModelTags.add(tag);
            }else{
                managerModelTags.add(tag);
            }
        }
        productRecord.setManagerModelTags(managerModelTags);


        //组装项目适合人群标签
        List<SystemDictionary> fitPersonDictionaries=dictionaryService.getDictionaryByKey("fit_person");
        if(productRecord.getFitPerson()==null)productRecord.setFitPerson("");
        String [] fitPersonTagIds = productRecord.getFitPerson().split(",");

        List<Map<String,Object>> fitPersonTags = new ArrayList<Map<String, Object>>();
        for(SystemDictionary dictionary:fitPersonDictionaries){
            Map<String,Object> tag=new HashMap<String,Object>();
            tag.put("id",dictionary.getId());
            tag.put("name",dictionary.getValue());
            int isChecked=0;

            for(String fitPersonTagId:fitPersonTagIds){
                if(fitPersonTagId.equalsIgnoreCase(String.valueOf(dictionary.getId()))){
                    isChecked=1;
                    break;
                }
            }
            tag.put("isChecked",isChecked);
            if(!isAll){
                if(isChecked==1)managerModelTags.add(tag);
            }else{
                fitPersonTags.add(tag);
            }
        }
        productRecord.setFitPersonTags(fitPersonTags);

        return productRecord;
    }
}
